package com.zcb.agrimarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcb.agrimarket.mapper.LabMapper;
import com.zcb.agrimarket.mapper.StudentApplyLogMapper;
import com.zcb.agrimarket.pojo.Arrange;
import com.zcb.agrimarket.pojo.Lab;
import com.zcb.agrimarket.pojo.StudentApplyLog;
import com.zcb.agrimarket.pojo.TeacherApplyData;
import com.zcb.agrimarket.service.ArrangeService;
import com.zcb.agrimarket.mapper.ArrangeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author KarrySmile
* @description 针对表【arrange(排课表)】的数据库操作Service实现
* @createDate 2024-04-05 22:51:37
*/
@Slf4j
@Service
public class ArrangeServiceImpl extends ServiceImpl<ArrangeMapper, Arrange>
    implements ArrangeService{

    @Autowired
    StudentApplyLogMapper studentApplyLogMapper;

    @Autowired
    ArrangeMapper arrangeMapper;

    @Override
    public void updataStudentApply(StudentApplyLog studentApplyLog) {
        log.info(String.valueOf(studentApplyLog));
        //审批通过
        if(studentApplyLog.getState()==1){
            studentApplyLogMapper.update(studentApplyLog);
            Arrange arrange = arrangeMapper.selectByStudentApplyLog(studentApplyLog);
            //之前都不存在申请记录
            if(arrange==null){
                Arrange arrange1 = new Arrange();
                arrange1.setSemesterId(studentApplyLog.getSemesterId());
                arrange1.setSemesterName(studentApplyLog.getSemesterName());
                arrange1.setLabId(studentApplyLog.getLabId());
                arrange1.setWeek(studentApplyLog.getWeek());
                arrange1.setDay(studentApplyLog.getDay());
                arrange1.setSession(studentApplyLog.getSession());
                arrangeMapper.insert(arrange1);
            }//如果之前存在申请记录则修改排课安排状态即可
            else if(arrange.getStatus()==0){
                arrange.setStatus(2);
                arrangeMapper.insert(arrange);
            }
        }//审批不通过
        else{
            studentApplyLogMapper.update(studentApplyLog);
        }
    }

    @Autowired
    LabMapper labMapper;

    @Override
    public void managementForTeacher(TeacherApplyData teacherApplyData) {
        Arrange arrange = new Arrange();
        arrange.setStatus(2);
        arrange.setTeacherId(teacherApplyData.getTeacherId());
        arrange.setTeacherName(teacherApplyData.getTeacherName());
        arrange.setSemesterId(teacherApplyData.getSemesterId());
        arrange.setSemesterName(teacherApplyData.getSemesterName());
        arrange.setCourseName(teacherApplyData.getCourseName());
        for(int week=teacherApplyData.getBeginWeek();week<=teacherApplyData.getEndWeek();week++){
            arrange.setWeek(week);
            //遍历组装并进行插入
            for(int i=0;i<teacherApplyData.getSession().length;i++) {
                int day = teacherApplyData.getSession()[i][0];
                int session = teacherApplyData.getSession()[i][1];
                arrange.setDay(day);
                arrange.setSession(session);
                for(Long LabId:teacherApplyData.getLabIds()[day][session]){
                    arrange.setLabId(LabId);
                    Lab lab = labMapper.selectById(LabId);
                    arrange.setLabName(lab.getName());
                    arrange.setLabName(lab.getLabTypeName());
                    arrange.setLabNum(lab.getLabNum());
                    arrangeMapper.insert(arrange);
                    arrange.setId(arrange.getId()+1);
                }
            }
        }
    }
}




